home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 43 / Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso / -serious- / programming / other / flexcat / lib / c++_catalogf.cc < prev    next >
C/C++ Source or Header  |  1999-06-14  |  3KB  |  102 lines

  1. //   Multipurpose CatalogF class implementation
  2. //   Written by Antonio J. Gomez Glez. on 20.6.94
  3. //   You MUST include CatalogF.h in your code. 
  4. //   If you use FlexCat, you only have to include the file generated
  5. //   with it.
  6.  
  7. #include "CatalogF.h"
  8.  
  9. extern "C" {
  10. #include <clib/locale_protos.h>
  11. #include <inline/locale.h>
  12. #include <clib/exec_protos.h>
  13. };
  14.  
  15. unsigned CatalogF::counter = 0;     // counter of open catalogs
  16. struct LocaleBase* LocaleBase = 0;  // We will try to open locale.library
  17.  
  18. // Constructor:
  19. // Needs:- filename of the catalog to be used. NEEDED!!
  20. //       - built in language of the catalog descriptor. defaults to "english"
  21. //       - language requested. Otherwise, it will use the user defined one.
  22. //       - requested version number for the catalog. defaults to any.
  23. //       - Locale (as returned by OpenLocale). defaults to user defined one.
  24. //
  25. // Try to open locale.library for the first CatalogF object declared. And
  26. // keeps with this with following objects.
  27. // Counts the number of defined objects.
  28. // And opens the catalog if avaible.
  29.  
  30. CatalogF::CatalogF( const STRPTR   catalogFileName,
  31.                     const STRPTR   builtInLanguage,
  32.                     const LONG     versionNumber,
  33.                     const STRPTR   languageName,
  34.                     struct Locale* loc )
  35. {
  36.   if ( counter == 0 )  // means that this is the first object
  37.   {
  38.      LocaleBase = (struct LocaleBase* )OpenLibrary("locale.library", 38L );
  39.   }
  40.   counter++;
  41.  
  42.   if ( LocaleBase != 0 )  
  43.   {                  // locale.library is avaible
  44.     LONG tag = TAG_IGNORE;  // in case language not provided use default
  45.     
  46.     if (languageName != 0)      // if language specified, use that
  47.     { 
  48.       tag = OC_Language;
  49.     }
  50.     thecatalog = OpenCatalog( loc,
  51.                               catalogFileName,
  52.                               OC_BuiltInLanguage, builtInLanguage,
  53.                               tag, languageName,
  54.                               OC_Version, versionNumber,
  55.                               TAG_DONE );
  56.   } // else use built-in strings                       
  57. }
  58.  
  59. // Destructor:
  60. // If locale.library was opened, try to close the catalog, even if no catalog
  61. // was opened (this is supported by CloseCatalog()).
  62. // When the counter of avaible objects reaches 0, it try to close locale.library
  63.  
  64. CatalogF::~CatalogF()
  65. {
  66.   counter--;
  67.   if ( LocaleBase != 0 )
  68.   { 
  69.     CloseCatalog( thecatalog );
  70.     if ( counter == 0 )
  71.     {
  72.       struct LocaleBase* lb = LocaleBase;
  73.       CloseLibrary( (struct Library* )lb );
  74.       LocaleBase = 0;
  75.     }
  76.   }
  77. }
  78.  
  79. // Retrive the string.
  80. // If there is locale.library and a opened catalog returns the catalog
  81. // string, else returns the built-in string.
  82. //
  83. // Needs a struct of type catMessage that contains the value ID and the
  84. // string itself. The name of this constant struct is the ID name. This way
  85. // we avoid the use of #define's or const, or any type of search ...
  86. // it returns a const pointer (STRPTR) to the string.
  87. // This method is constant so CatalogFs object can be declared to be const
  88.  
  89. const STRPTR
  90. CatalogF::GetStr(const CatMessage& mess) const
  91. {
  92.   if ( LocaleBase == 0 )
  93.   {
  94.      return( mess.textstring );
  95.   }
  96.   else
  97.   {
  98.      return( GetCatalogStr(thecatalog, mess.ID, mess.textstring) );
  99.   }
  100. }
  101.  
  102.